﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using JCNT.DAL;
using System.IO;
using Raize.CodeSiteLogging;

namespace JCNT.NoteApp
{
    public partial class FPic : Form
    {
        CPic cpic = new CPic();
        int i_pic = 0;
        public String[] ID_Lst;
        public FPic()
        {
            InitializeComponent();
        }

        private void FPic_Load(object sender, EventArgs e)
        {
            tv1.Dock = DockStyle.Fill;
            lv1.Dock = DockStyle.Fill;
            panel2.Dock = DockStyle.Fill;
            picBox1.Left = 0;
            picBox1.Top = 0;
            panel2.Visible = false;
            cpic.fillTree(tv1, null, "0");
            picBox1.MouseWheel += new MouseEventHandler(panel1_MouseWheel);

        }

        void panel1_MouseWheel(object sender, MouseEventArgs e)
        {
            try
            {
                if (panel2.Visible == false) return;
                int pic_Index = 0;
                Boolean b_find = false;
                labIndex.Text = "";
                //if (i_pic > cpic.wp_pics.Length - 1) i_pic = cpic.wp_pics.Length - 1;
                String ID = Convert.ToString(picBox1.Tag);
                if (ID_Lst == null) return;
                for (int i = 0; i < ID_Lst.Length; i++)
                {
                    if (ID_Lst[i] == ID.ToString())
                    {
                        pic_Index = i;
                        b_find = true;
                        break;
                    }
                }
                if (b_find == false) return;

                if (e.Delta < 0) // 向下滚动
                {
                    if (pic_Index < ID_Lst.Length - 1) pic_Index++;
                }
                else                    // 向上滚动
                {
                    if (pic_Index > 0) pic_Index--;
                }

                if (pic_Index < 0) pic_Index = 0;
                if (pic_Index >= ID_Lst.Length) pic_Index = ID_Lst.Length - 1;
                labIndex.Text = pic_Index.ToString();
                ID = ID_Lst[pic_Index];

                labID.Text = ID.ToString();
                picBox1.Image = cpic.getPic(ID);
                picBox1.Width = picBox1.Image.Width;
                picBox1.Height = picBox1.Image.Height;
                picBox1.Tag = ID;
            }
            catch (System.Exception ex)
            {
                CodeSite.Send(ex.ToString());
                CodeSite.Send("void panel1_MouseWheel(object sender, MouseEventArgs e)", ex);
            }

        }

        private void tv1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            try
            {
                string ID = (string)tv1.SelectedNode.Tag;
                if (tv1.SelectedNode.ImageIndex == 1)
                {
                    i_pic = Convert.ToInt16(ID);
                    picBox1.Image = cpic.getPic(ID);
                    cpic.setIco(ID);
                    picBox1.Width = picBox1.Image.Width;
                    picBox1.Height = picBox1.Image.Height;
                    picBox1.Tag = ID;
                    labNam.Text = cpic.wp_pic.Nam;
                    labID.Text = cpic.wp_pic.ID;
                    labLen.Text = picBox1.Image.Size.ToString();
                    panel2.Visible = true;
                    lv1.Visible = false;
                    if (cpic.wp_pics.Length == 0 || cpic.wp_pics[0].RootID != cpic.wp_pic.RootID)
                    {
                        cpic.getPics(cpic.wp_pic.RootID);
                    }
                }
                if (tv1.SelectedNode.ImageIndex == 0)
                {
                    i_pic = 0;
                    ID_Lst = cpic.fillLv(lv1, imageList1, ID);
                    panel2.Visible = false;
                    lv1.Visible = true;
                }
            }
            catch (System.Exception ex)
            {
                CodeSite.Send(ex.ToString());
                CodeSite.Send("private void tv1_AfterSelect(object sender, TreeViewEventArgs e)", ex);
            }

        }

        private void lv1_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                if (lv1.SelectedItems.Count == 0) return;
                string ID = (string)lv1.SelectedItems[0].Tag;
                i_pic = Convert.ToInt16(ID) - 1;
                labID.Text = i_pic.ToString();
                picBox1.Image = cpic.getPic(ID);
                picBox1.Width = picBox1.Image.Width;
                picBox1.Height = picBox1.Image.Height;
                picBox1.Tag = ID;
                panel2.Visible = true;
                cpic.setIco(ID);
            }
            catch (System.Exception ex)
            {
                CodeSite.Send(ex.ToString());
                CodeSite.Send("private void lv1_DoubleClick(object sender, EventArgs e)", ex);
            }

        }

        private void picBox1_MouseEnter(object sender, EventArgs e)
        {
            picBox1.Focus();
        }

        private void btn_Click(object sender, EventArgs e)
        {
            int i_btn = 0;
            if (sender is ToolStripButton)
            {
                i_btn = Convert.ToInt16(((ToolStripButton)sender).Tag);
            }

            switch (i_btn)
            {
                case 1:     //show listview
                    lv1.Visible = true;
                    panel2.Visible = false;
                    break;
                case 2:     //save picture to file
                    if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                    {
                        string fnam = saveFileDialog1.FileName;
                        picBox1.Image.Save(fnam, System.Drawing.Imaging.ImageFormat.Jpeg);
                    }

                    break;
            }
        }
    }
}
